package com.ylsk.check.service.impl;

import java.util.Date;
import java.util.List;

import com.google.common.primitives.Longs;
import com.ylsk.check.domain.ReportInfo;
import com.ylsk.check.domain.WithdrawalReport;
import com.ylsk.check.domain.dto.ChangeReportDTO;
import com.ylsk.check.domain.dto.MsgTemplateDTO;
import com.ylsk.check.domain.emum.MsgTemplateEnum;
import com.ylsk.check.domain.so.ChangeReportSO;
import com.ylsk.check.domain.vo.ChangeReportVO;
import com.ylsk.common.annotation.DataScope;
import com.ylsk.common.config.SnowIdConfig;
import com.ylsk.common.exception.ServiceException;
import com.ylsk.common.utils.DateUtils;
import com.ylsk.common.utils.SecurityUtils;
import com.ylsk.common.utils.StringUtils;
import com.ylsk.system.domain.FileSimple;
import com.ylsk.system.helper.FileHelper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ylsk.check.mapper.ChangeReportMapper;
import com.ylsk.check.domain.ChangeReport;
import com.ylsk.check.service.IChangeReportService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 变动上报Service业务层处理
 *
 * @author yangsy
 * @date 2024-01-08
 */
@Service
public class ChangeReportServiceImpl implements IChangeReportService
{
    @Autowired
    private ChangeReportMapper changeReportMapper;

    @Autowired
    private SnowIdConfig snowIdConfig;

    @Autowired
    private FileHelper fileHelper;

    @Autowired
    private ReportInfoHelper helper;


    /**
     * 查询变动上报
     *
     * @param changeReportId 变动上报主键
     * @return 变动上报
     */
    @Override
    public ChangeReportVO selectChangeReportByChangeReportId(Long changeReportId)
    {
        ChangeReportVO changeReportVO = changeReportMapper.selectChangeReportByChangeReportId(changeReportId);
        //设置附件信息
        Long changeFileId = changeReportVO.getChangeFileId();
        if(null != changeFileId){
            List<FileSimple> fileSimpleList =fileHelper.getFileByBusinessId(changeFileId);
            changeReportVO.setChangeFile(fileSimpleList);
        }
        Long licenseFileId = changeReportVO.getLicenseFileId();
        if(null != licenseFileId){
            List<FileSimple> fileSimpleList =fileHelper.getFileByBusinessId(licenseFileId);
            changeReportVO.setLicenseFile(fileSimpleList);
        }
        return changeReportVO;
    }

    /**
     * 查询变动上报列表
     *
     * @param changeReportSO 变动上报
     * @return 变动上报
     */
    @Override
    @DataScope(deptAlias = "sd",userAlias = "su")
    public List<ChangeReportVO> selectChangeReportList(ChangeReportSO changeReportSO)
    {
        return changeReportMapper.selectChangeReportList(changeReportSO);
    }

    /**
     * 新增变动上报
     *
     * @param changeReportDTO 变动上报
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertChangeReport(ChangeReportDTO changeReportDTO)
    {
        ChangeReport changeReport = new ChangeReport();
        BeanUtils.copyProperties(changeReportDTO,changeReport);
        final Long reportInfoId = changeReportDTO.getReportInfoId();
        ReportInfo reportInfo = helper.getReportInfo(reportInfoId);
        //  附件信息
        String changeFile = changeReportDTO.getChangeFile();
        long changeFileId = snowIdConfig.getSnowId();
        if(StringUtils.isNotEmpty(changeFile)){
            fileHelper.saveBusinessFileData(changeFile,changeFileId);
        }

        String licenseFile = changeReportDTO.getLicenseFile();
        long licenseFileId = snowIdConfig.getSnowId();
        if(StringUtils.isNotEmpty(changeFile)){
            fileHelper.saveBusinessFileData(licenseFile,licenseFileId);
        }

        //  提交消息
        MsgTemplateDTO msgTemplateDTO = new MsgTemplateDTO(MsgTemplateEnum.TEMPLATE_BDSB,SecurityUtils.getUserId(), Longs.asList(reportInfo.getReviewedBy()));
        String p1 = SecurityUtils.getLoginUser().getUser().getNickName();
        String p2 = DateUtils.dateTimeNow("yyyy-MM-dd HH:mm:ss");
        String p3 = reportInfo.getProjectName();
        String p4 = DateUtils.dateTime(changeReport.getChangeTime());
        helper.saveMsg(msgTemplateDTO,p1,p2,p3,p4);

        changeReport.setChangeFileId(changeFileId);
        changeReport.setLicenseFileId(licenseFileId);
        changeReport.setDeptId(reportInfo.getDeptId());
        changeReport.setReviewedBy(reportInfo.getReviewedBy());
        changeReport.setChangeReportId(snowIdConfig.getSnowId());
        return changeReportMapper.insert(changeReport);
    }

    /**
     * 修改变动上报
     *
     * @param changeReportDTO 变动上报
     * @return 结果
     */
    @Override
    public int updateChangeReport(ChangeReportDTO changeReportDTO)
    {
        Long changeReportId = changeReportDTO.getChangeReportId();
        if(null == changeReportId){
            throw new ServiceException("changeReportId 不能为空!");
        }
        ChangeReport changeReport = new ChangeReport();
        BeanUtils.copyProperties(changeReportDTO,changeReport);
        //更新附件信息
        String changeFile = changeReportDTO.getChangeFile();
        ChangeReport _changeReport = changeReportMapper.selectById(changeReportId);
        fileHelper.updateBusinessFileData(changeFile,_changeReport.getChangeFileId());
        String licenseFile = changeReportDTO.getLicenseFile();
        fileHelper.updateBusinessFileData(licenseFile,_changeReport.getLicenseFileId());
        return changeReportMapper.updateById(changeReport);
    }

    /**
     * 批量删除变动上报
     *
     * @param changeReportIds 需要删除的变动上报主键
     * @return 结果
     */
    @Override
    public int deleteChangeReportByChangeReportIds(Long[] changeReportIds)
    {
        return changeReportMapper.deleteChangeReportByChangeReportIds(changeReportIds);
    }

    /**
     * 删除变动上报信息
     *
     * @param changeReportId 变动上报主键
     * @return 结果
     */
    @Override
    public int deleteChangeReportByChangeReportId(Long changeReportId)
    {
        return changeReportMapper.deleteChangeReportByChangeReportId(changeReportId);
    }
}
